<?php

class add_edit_nnci extends a_admin_controller {

    protected $srv;
    protected $edit_obj;
    protected $nncid_id;
    protected $message;
    protected $msg_index;
    protected $is_edit;

    protected $nnci_products;
    protected $count;
    protected $page_link;


    public function __construct() {
        parent::__construct(FALSE);
        $this->srv = new nnci_l_product;
        $this->edit_obj = $this->srv->new_obj();
        $this->is_edit=0;
        $this->set_message();

        $this->segment();
    }

    public function batch_assign() {
        $this->smarty()->assign("edit_obj", $this->edit_obj);
        $this->smarty()->assign("msg", $this->msg);
        $this->smarty()->assign("message", $this->message);
        $this->smarty()->assign("is_edit", $this->is_edit);

        $this->assign("nnci_products", $this->nnci_products);
        $this->assign("count", $this->count);
        $this->assign("page_link", $this->page_link);
    }

    public function index()
    {
        if($this->input->post("edit"))
        {
            if($this->is_valid()==1)
                $this->edit();
        }
        else if($this->input->post("add"))
        {
            if($this->is_valid()==1)
                $this->add();
        }
        
        $this->obj = $this->srv->return_table();
    }

    private function is_valid()
    {
        $valid = 0;
        if(strcmp(trim($this->input->post("nnc_id")), "")==0)
            $this->msg=4;
        else if(strcmp(trim($this->input->post("desc")), "")==0)
            $this->msg=5;
        else
            $valid = 1;

        return $valid;
    }

    public function select_nnic($id)
    {
        $this->edit_obj = nnci_l_product::get_by_id($id);
        $this->is_edit=1;
    }

    public function edit()
    {
        if(nnci_l_product::nncid_exists($this->input->post("nnc_id"), $this->input->post("id")))
        {
            nnci_l_product::save_nncid($this->input->post("id"), $this->input->post("nnc_id"), $this->input->post("desc"));
            $this->is_edit=0;
            $this->msg=3;
        }
        else
        {
            $this->is_edit=1;
            $this->edit_obj = nnci_l_product::get_by_id($this->input->post("id"));
            $this->msg=2;
        }
    }

    public function add()
    {
        if(nnci_l_product::nncid_exists($this->input->post("nnc_id")))
            $this->msg=2;
        else
        {
            nnci_l_product::create_nncid($this->input->post("nnc_id"), $this->input->post("desc"));
            $this->msg=1;
        }
        
    }

    private function set_message()
    {
        $this->message = array();
        $this->message[1][0] = "limegreen";
        $this->message[1][1] = "NNC Item ID entered successfully!";
        $this->message[2][0] = "red";
        $this->message[2][1] = "NNC Item ID already exists. Please enter a new ID";
        $this->message[3][0] = "limegreen";
        $this->message[3][1] = "NNC Item ID edited successfully!";
        $this->message[4][0] = "red";
        $this->message[4][1] = "NNC Item ID can not be empty";
        $this->message[5][0] = "red";
        $this->message[5][1] = "Description can not be empty";
        $this->msg=0;
    }

    public function segment($offset=0)
    {
        $offset = starts_with($offset,"p~" ) ? 0: $offset;
        $processor  = new search_processor_l_search("PRODUCT_NNCI",TRUE);

        $param = new dynamic_param_l_search(microtime(), module_u_search::SEARCH_TYPE_PARAM );
        $param->set_select_line("*");
        $processor->add_param($param);

        $url_params = $processor->add_params_from_url();
        $url_tail = regenerate_url_for_paging($url_params);

        $this->count = $processor->count_result();
        $this->page_link = get_pagigination("product/admin/add_edit_nnci/segment/", $this->count,  20,$url_tail, 5);
        $this->nnci_products = $processor->get_result(new nnci_m_product(),$offset,  20);
    }

    //adding record from the cvs file
    private function add_record()
    {
	$file = fopen("nnci.csv", "r") or exit("Unable to open file!");

	//Output a line of the file until the end is reached
	while(!feof($file))
	{
		$id = "";
		$desc = "";

		$tok = strtok(fgets($file), ",");
		$id = $tok;

		while ($tok !== false) {
			$tok = strtok("");
			$desc = $desc.$tok;
		}

            nnci_l_product::create_nncid($id, $desc);
	}

	fclose($file);
    }

}